package com.techwells.learningkites.adapter;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import com.techwells.learningkites.R;
import com.techwells.learningkites.controller.SegmentDetailActivity;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.CourseCategory;
import com.techwells.learningkites.networkservice.model.Dialogue;
import com.techwells.learningkites.networkservice.model.Sentences;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.SoundPool;
import android.text.TextUtils.EllipsizeCallback;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DialogListAdapter extends BaseAdapter {
	
	private Context mContext;
	private LayoutInflater mInflater;
	private List<Sentences> mList;
	private MediaPlayer mp;

	
	public DialogListAdapter(Context mContext, List<Sentences> mList) {
		super();
		this.mContext = mContext;
		mInflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mList = mList;
		mp = new MediaPlayer();
	}

	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		CellHolder cellHolder;
        if (convertView == null) {
        	cellHolder = new CellHolder();
        		convertView = mInflater.inflate(R.layout.item_dialoglist, null);
        		cellHolder.num = (TextView) convertView.findViewById(R.id.num);
        		cellHolder.enLang = (TextView) convertView.findViewById(R.id.en_lang);
        		cellHolder.cnLang = (TextView) convertView.findViewById(R.id.cn_lang);
        		cellHolder.sound = (ImageView) convertView.findViewById(R.id.sound);
        		cellHolder.favorite = (ImageView) convertView.findViewById(R.id.img_favorite);
                convertView.setTag(cellHolder);

        } else {
        	cellHolder = (CellHolder) convertView.getTag();
        }        
       
        if (mList.get(position).sequence < 10) {
        	cellHolder.num.setText(mList.get(position).sequence + "." + "\t");
		}else {
			cellHolder.num.setText(""+mList.get(position).sequence + ".");
		}
        
        String enString = mList.get(position).enLang.replaceAll("\r|\n", "");
        String cnString = mList.get(position).cnLang.replaceAll("\r|\n", "");
        cellHolder.enLang.setText(enString);
        cellHolder.cnLang.setText(cnString);
        
        cellHolder.sound.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					if (mp.isPlaying()) {
						mp.stop();
					}
					mp.reset();
					mp.setDataSource(KitesRequestUrl.BASE_URL_IMG + mList.get(position).resource);
					mp.prepare();
					mp.setOnPreparedListener(new OnPreparedListener() {
						
						@Override
						public void onPrepared(MediaPlayer mp) {
							mp.start();
							
						}
					});
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
        
        cellHolder.favorite.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				HashMap<String, Object> argMap = new HashMap<String, Object>();	
				argMap.put("videoSentenceId", mList.get(position).id);
				((SegmentDetailActivity) mContext).doTask(KitesServiceMediator.SERVICE_DO_VIDEO_SENTENCE,argMap);
				
			}
		});
        
		return convertView;
	}

	
	public static class CellHolder implements Serializable{
    	TextView num,dialogue,enLang,cnLang;
    	ImageView sound,favorite;
    }
}
